<?php
/**
 * Created by PhpStorm.
 * User: River
 * Email: river0428@gmail.com
 * Date: 2018/11/13
 * Time: 下午1:10
 */

namespace Modules\Api\Controllers;

use App\Models\Banner;
use App\Models\Configure;
use App\Models\Notice;
use Carbon\Carbon;
use EasyWeChat\Factory;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function index()
    {
        $res = file_get_contents(storage_path('app/notice.txt'));

        $res   = explode("\r\n", $res);
        $items = [];
        $m     = 10;
        foreach ($res as $key => $item) {
            $val     = explode("\t", $item);
            $items[] = [
                'id'         => $key + 1,
                'content'    => $val[0] . '充值了' . $val[1],
                'created_at' => Carbon::now()->subMinutes($m)->diffForHumans()
            ];
            $m       += mt_rand(0, 9);
        }

        $data['notice'] = $items;
        $data['banner'] = [];


        $banner = Banner::orderBy('list_order', 'asc')->with('image')->get();
        foreach ($banner as $item) {
            $data['banner'][] = [
                'image'       => $item->image->file_url,
                'width'       => $item->image->width,
                'height'      => $item->image->height,
                'redirect_to' => $item->redirect_to,
                'url'=>$item->url,
            ];
        }

        return success($data);
    }

    public function faxian()
    {
        $arr1 = array('http://h5.jiayouhui360.com/img/hyfl01.87a1092c.png','http://h5.jiayouhui360.com/img/hyfl02.215b5720.png','http://h5.jiayouhui360.com/img/hyfl03.24b82c86.png','http://h5.jiayouhui360.com/img/hb_20190305163745.png');
        $arr2 = array('邀请有礼，多邀多得','双十一任性购，红包送不停','国庆出游季','喜迎中秋');
        $arr3 = array('http://h5.jiayouhui360.com/Share','','','');
        $arr4 = array('进行中','已结束','已结束','已结束');
        foreach($arr1 as $key=>$val){

            $d[$key]['img']=$arr1[$key];

            $d[$key]['title']=$arr2[$key];

            $d[$key]['url']=$arr3[$key];

            $d[$key]['status']=$arr4[$key];
        }
        // $payAt = Carbon::parse('+1 months')->toDateTimeString();
        // var_dump($payAt);
//        $jsonObj_1 = json_encode($d,JSON_UNESCAPED_SLASHES);
        return success($d);
    }

    public function share()
    {
        $data = [
            'share_title' => Configure::getByKey('share_title'),
            'share_desc'  => Configure::getByKey('share_desc'),
            'share_img'   => Configure::getByKey('share_img'),
            'share_link'  => Configure::getByKey('share_link')
        ];

        return success($data);
    }

    public function jssdk(Request $request)
    {
        $app  = Factory::officialAccount(config('wechat'));
        $APIs = [
            'updateAppMessageShareData',
            'updateTimelineShareData',
            'chooseImage',
            'previewImage',
            'uploadImage',
            'downloadImage',
            'getNetworkType',
            'openLocation',
            'getLocation',
            'hideOptionMenu',
            'showOptionMenu',
            'hideMenuItems',
            'showMenuItems',
            'hideAllNonBaseMenuItem',
            'showAllNonBaseMenuItem',
            'closeWindow',
        ];
        $url = $request->input('url');
        $app->jssdk->setUrl($url);
        $data = $app->jssdk->buildConfig($APIs, $debug = false, $beta = false, $json = false);

        return success($data);
    }
}